﻿define(['durandal/system', 'plugins/router', 'services/logger','services/appsecurity', 'services/errorhandler'],
    function (system, router, logger, appsecurity, errorhandler) {
        var shell = {
            activate: activate,
            router: router
        };
        
        return shell;

        //#region Internal Methods
        function activate() {
            return boot();
        }

        function boot() {
            log('Photograp loaded!', null, true);

            //configure routing
            router.makeRelative({ moduleId: 'viewmodels' });

            // If the route has the authorize flag and the user is not logged in => navigate to login view                                
            router.guardRoute = function (instance, instruction) {
                if (instruction.config.authorize) {
                    if (appsecurity.user().IsAuthenticated && appsecurity.isUserInRole(instruction.config.authorize)) {
                        return true
                    } else {
                        return "/account/login?redirectto=" + instruction.fragment;
                    }
                }
                return true;
            }
            return router.map([
                            // Nav urls
                            { route: ['', 'home*albums'], moduleId: 'home/index', title: 'Home', nav: true, hash: "#home" },

                            //{ route: 'home/albums', moduleId: 'home/albums/index', title: 'Album photo', type: 'intro', nav: false },
                            //{ route: 'home/articles', moduleId: 'home/articles', title: 'Articles', nav: true, hash: "#home/articles" },

                            { route: 'home/details', moduleId: 'home/details', title: 'About', nav: true, hash: "#home/details" },
                            { route: 'notfound', moduleId: 'notfound', title: 'Not found', nav: false },



                            // Admin panel url
                            //{ route: 'admin/panel', moduleId: 'admin/panel', title: 'Admin Panel', nav: false, hash: "#admin/panel", authorize: ["Administrator"] },
                            // Account Controller urls
                            { route: 'account/login', moduleId: 'account/login', title: 'Login', nav: false, hash: "#account/login" },
                            //{ route: 'account/externalloginconfirmation', moduleId: 'account/externalloginconfirmation', title: 'External login confirmation', nav: false, hash: "#account/externalloginconfirmation" },
                            //{ route: 'account/externalloginfailure', moduleId: 'account/externalloginfailure', title: 'External login failure', nav: false, hash: "#account/externalloginfailure" },
                            { route: 'account/register', moduleId: 'account/register', title: 'Register', nav: false, hash: "#account/register" },
                            { route: 'account/manage', moduleId: 'account/manage', title: 'Manage account', nav: false, hash: "#account/manage", authorize: ["User", "Administrator"] },

                            // User articles urls
                            //{ route: 'user/dashboard', moduleId: 'user/dashboard', title: 'Dashboard', nav: false, hash: "#user/dashboard", authorize: ["User"] },
                            //{ route: ':createdby/:categorycode/:articlecode', moduleId: 'user/article', title: 'Article', nav: false },
                            ])

                        .buildNavigationModel()
                        .mapUnknownRoutes("notfound", "notfound")
                        .activate({ pushState: true });
        }

        function log(msg, data, showToast) {
            logger.log(msg, data, system.getModuleId(shell), showToast);
        }

        function logError(msg, data, showToast) {
            logger.logError(msg, data, system.getModuleId(shell), showToast);
        }
        //#endregion
    });